//
//  BMPluginController.m
//  snomPhone
//
//  Created by Daniel Hazelbaker on 4/3/09.
//  Copyright 2009 High Desert Church. All rights reserved.
//

#import "BMPluginController.h"
#import "BMPreferences.h"
#import "preferences.h"
#import "comBlueboxmoonPhoneiTunes.h"
#import "comBlueboxmoonPhoneABLookup.h"
#import "comBlueBoxmoonPhoneWebABLookup.h"
#import "comBlueboxmoonPhoneWebAddress.h"
#import "comBlueboxmoonPhoneHover.h"


@implementation BMPluginController


@synthesize phoneAddress, phonePassword;


//
// Initialize a new plugin controller. Find and load all the plugins available
// and activate all plugins that are listed in the preferences.
//
- (id)initFromBundle:(NSBundle *)coreBundle;
{
    if ([super init] == nil)
	return nil;

    //
    // Prepare the arrays of plugins.
    //
    addressHandlers = [[NSMutableArray alloc] init];
    lookupHandlers = [[NSMutableArray alloc] init];
    callHandlers = [[NSMutableArray alloc] init];

    //
    // Activate the proper plugins
    //
    [self prepareHandlers];

    return self;
}


//
// Activate all handlers.
//
- (void)prepareHandlers
{
    //
    // Initialize all the address handlers.
    //
    [comBlueboxmoonPhoneWebAddress setDefaultPreferences];
    [addressHandlers addObject:[[[comBlueboxmoonPhoneWebAddress alloc] init] autorelease]];

    //
    // Initialize all the person lookup handlers.
    //
    [comBlueboxmoonPhoneWebABLookup setDefaultPreferences];
    [lookupHandlers addObject:[[[comBlueboxmoonPhoneWebABLookup alloc] init] autorelease]];
    [comBlueboxmoonPhoneABLookup setDefaultPreferences];
    [lookupHandlers addObject:[[[comBlueboxmoonPhoneABLookup alloc] init] autorelease]];

    //
    // Initialize all the during call handlers.
    //
    [comBlueboxmoonPhoneiTunes setDefaultPreferences];
    [callHandlers addObject:[[[comBlueboxmoonPhoneiTunes alloc] init] autorelease]];
    [comBlueboxmoonPhoneHover setDefaultPreferences];
    [callHandlers addObject:[[[comBlueboxmoonPhoneHover alloc] init] autorelease]];
}



#pragma mark BMPluginHandlerProtocol methods.
////////////////////////////////////////////////////////////////////////////////////
//
// BMPluginHandlerProtocol methods.
//
////////////////////////////////////////////////////////////////////////////////////


//
// The phone address lookup continues until the first plugin returns a
// non-nil result, indicating that it found the IP address (or host name)
// we are looking for.
//
- (NSString *)lookupPhoneAddress
{
    NSString	*ipAddress;
    int		i;
    id		handler;


    //
    // Walk the handler list.
    //
    for (i = 0; i < [addressHandlers count]; i++) {
	handler = [addressHandlers objectAtIndex:i];
	if ([handler respondsToSelector:@selector(lookupPhoneAddress)] == YES) {
	    if ((ipAddress = [handler lookupPhoneAddress]) != nil)
		return ipAddress;
	}
    }

    return nil;
}


//
// Phone number lookups continue until the first plugin returns a non-nil
// result, indicating that it found the phone number.
//
- (NSDictionary *)lookupPersonForPhoneNumbers:(NSArray *)phoneNumbers
{
    NSDictionary    *person;
    int		    i;
    id		    handler;


    //
    // Walk the handler list.
    //
    for (i = 0; i < [lookupHandlers count]; i++) {
	handler = [lookupHandlers objectAtIndex:i];
	if ([handler respondsToSelector:@selector(lookupPersonForPhoneNumbers:)] == YES) {
	    if ((person = [handler lookupPersonForPhoneNumbers:phoneNumbers]) != nil)
		return person;
	}
    }

    return nil;
}


//
// Notification continues through all plugins until all plugins have been
// notified. The phoneNumber will never be nil while the person may be nil
// if no lookup found a match.
//
- (void)notifyIncomingCall:(SSCall *)call
{
    int	i;
    id	handler;


    //
    // Walk the handler list.
    //
    for (i = 0; i < [callHandlers count]; i++) {
	handler = [callHandlers objectAtIndex:i];
	if ([handler respondsToSelector:@selector(notifyIncomingCall:)] == YES) {
	    [handler notifyIncomingCall:call];
	}
    }
    
    
}


//
// Notification continues through all plugins until all plugins have been
// notified. The phoneNumber will never be nil while the person may be nil
// if no lookup found a match.
//
- (void)notifyOutgoingCall:(SSCall *)call
{
    int	i;
    id	handler;


    //
    // Walk the handler list.
    //
    for (i = 0; i < [callHandlers count]; i++) {
	handler = [callHandlers objectAtIndex:i];
	if ([handler respondsToSelector:@selector(notifyOutgoingCall:)] == YES) {
	    [handler notifyOutgoingCall:call];
	}
    }
}


//
// Notification that the call has ended. All handlers are notified.
//
- (void)notifyCallEnded:(SSCall *)call
{
    int	i;
    id	handler;


    //
    // Walk the handler list.
    //
    for (i = 0; i < [callHandlers count]; i++) {
	handler = [callHandlers objectAtIndex:i];
	if ([handler respondsToSelector:@selector(notifyCallEnded:)] == YES) {
	    [handler notifyCallEnded:call];
	}
    }
}


@end
